package co.edu.unal.geditor.view;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;

import co.edu.unal.geditor.controller.App;
import co.edu.unal.geditor.model.BoundBox;
import co.edu.unal.geditor.view.controlpoints.LineCenterControlPoint;
import co.edu.unal.geditor.view.tools.Tool;

public class LineBoundBoxView extends BoundBoxView {

	public LineBoundBoxView(BoundBox Owner) {
		super(Owner);
		isLine = true;
		
	}
	@Override
	protected void registerControlPoints(){
		m_lControlPoints.add(m_cpLU);
		m_lControlPoints.add(m_cpRD);
		
	}

	@Override
	public void chageCpPoints() {
		m_cpLU.changePoint(m_owner.getLeftUp());
		m_cpRD.changePoint(m_owner.getRightDown());

	}
	
	
	@Override
	public void paint(Graphics g) {
		chageCpPoints();
		cp_Center.changePoint(new Point(m_owner.getLeftUp().x-((m_owner.getLeftUp().x-m_owner.getRightDown().x)/2),m_owner.getLeftUp().y -((m_owner.getLeftUp().y-m_owner.getRightDown().y)/2)));
		Point ul = m_owner.getLeftUp();
		Point bd = m_owner.getRightDown();
	
		// casting (!) because of a Java design error ...
		Graphics2D gg = (Graphics2D) g;
	
		gg.setColor(DEF_SELECTED_COLOR);
	
		// 1. save active stroke
		Stroke stroke = gg.getStroke();
	
		// 2. change active stroke to dashed
		gg
				.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,
						BasicStroke.JOIN_MITER, 10.0f, DEF_DASHED_PATTERN,
						DEF_DASHED_));
		gg.drawLine(ul.x, ul.y, bd.x + 1, bd.y + 1);
	
		// 3. recover active stroke
		gg.setStroke(stroke);
		m_cpLU.paint(gg);
		m_cpRD.paint(gg);
		cp_Center.paint(gg);
	}

	@Override
	public boolean contain(Point ptContained) {
		boolean retorno = false;
			if (cp_Center.contain(ptContained) && m_owner.isSelected()) {
				App.getInstance().getCanvas().getActiveTool().setActiveAction(
						Tool.MOVE);
				cp_Center.changeCursor();
				retorno = true;
			}
		return (retorno);
	}
	protected LineCenterControlPoint cp_Center=new LineCenterControlPoint();

}
